﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Entity.SystemManage;
using YiSha.Model.Param.SystemManage;
using YiSha.Service.SystemManage;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Data.Repository;
using YiSha.Business.Cache;
using System.Linq.Expressions;
using YiSha.Enums;

namespace YiSha.Service.SystemManage
{
    public class DataDictDetailService : RepositoryFactory
    {
        public DataDictDetailCache DataDictDetailCache { get; set; }


        #region 获取数据
        public async Task<List<DataDictDetailEntity>> GetList(DataDictDetailListParam param)
        {
            var expression = ListFilter(param);
            var list = SqlSugarAdapters.GetList(expression);
            return list.OrderBy(p => p.DictSort).ToList();
        }

        public async Task<List<DataDictDetailEntity>> GetPageList(DataDictDetailListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = SqlSugarAdapters.GetPager(expression, pagination);
            return list.Item2;
        }

        public async Task<DataDictDetailEntity> GetEntity(string id)
        {
            return SqlSugarAdapters.GetEntity<DataDictDetailEntity>(s => s.Id == id);
        }

        public async Task<int> GetMaxSort()
        {
            object result = SqlSugarAdapters.SqlQueryCount("SELECT MAX(DictSort) FROM SysDataDictDetail");
            int sort = result.ParseToInt();
            sort++;
            return sort;
        }

        public bool ExistDictKeyValue(DataDictDetailEntity entity)
        {
            var expression = ExtLinq.True<DataDictDetailEntity>();
            expression = expression.And(t => t.Valid);
            if (entity.Id.IsEmpty())
            {
                expression = expression.And(t => t.DictType == entity.DictType && (t.DictKey == entity.DictKey || t.DictValue == entity.DictValue));
            }
            else
            {
                expression = expression.And(t => t.DictType == entity.DictType && (t.DictKey == entity.DictKey || t.DictValue == entity.DictValue) && t.Id != entity.Id);
            }
            return SqlSugarAdapters.GetList(expression).Count() > 0 ? true : false;
        }
        #endregion

        #region 提交数据
        public async Task<TData> SaveForm(DataDictDetailEntity entity)
        {
            TData obj = new TData();
            if (entity.DictKey <= 0)
            {
                obj.Msg = "字典键必须大于0";
                obj.MsgCode = MsgCode.Fail;
                return obj;
            }
            if (ExistDictKeyValue(entity))
            {
                obj.Msg = "字典键或值已经存在！";
                obj.MsgCode = MsgCode.Fail;
                return obj;
            }
            if (entity.Id.IsEmpty())
            {
                await entity.Create();
                SqlSugarAdapters.Insert<DataDictDetailEntity>(entity);
            }
            else
            {
                await entity.Modify();
                SqlSugarAdapters.Update<DataDictDetailEntity>(entity);
            }
            DataDictDetailCache.Remove();
            obj.MsgCode = MsgCode.Success;
            return obj;
        }

        public async Task<TData> DeleteForm(List<string> ids)
        {
            TData obj = new TData();
            var info = SqlSugarAdapters.GetList<MenuEntity>(s => ids.Contains(s.Id));
            if (info.Count > 0)
            {
                SqlSugarAdapters.Delete<MenuEntity>(s => ids.Contains(s.Id));
            }
            else
            {
                obj.MsgCode = MsgCode.Fail;
                obj.Msg = "当前数据不存在";
                return obj;
            }
            SqlSugarAdapters.Delete<DataDictDetailEntity>(s => ids.Contains(s.Id));
            DataDictDetailCache.Remove();
            return obj;
        }
        #endregion

        #region 私有方法
        private Expression<Func<DataDictDetailEntity, bool>> ListFilter(DataDictDetailListParam param)
        {
            var expression = ExtLinq.True<DataDictDetailEntity>();
            if (param != null)
            {
                if (param.DictKey.ParseToInt() > 0)
                {
                    expression = expression.And(t => t.DictKey == param.DictKey);
                }

                if (!string.IsNullOrEmpty(param.DictValue))
                {
                    expression = expression.And(t => t.DictValue.Contains(param.DictValue));
                }

                if (!string.IsNullOrEmpty(param.DictType))
                {
                    expression = expression.And(t => t.DictType.Contains(param.DictType));
                }
            }
            return expression;
        }
        #endregion
    }
}
